<?php
/*下单http://www.12301.cc/meituan/MT_All_Order.php?mobile=15959110796&orderid=111101&outergoodsid=113|5488|10696&num=1&method=send&item_title=%E8%93%9D%E8%B0%83%E5%BA%84%E5%9B%AD%E6%B8%A9%E6%B3%89%E9%97%A8%E7%A5%A8&consume_type=1&sms_template=&mms_template=&token=353250145660&timestamp=1402987039&valid_start=2015-01-19+14%3A37%3A19&valid_ends=1970-01-01+08%3A00%3A00&app_key=ea59253a4292cbff10e3bb1ba1db01a490aabcf0&sign=bcc53d2bc980f564d3cf34ef99e078f63484f315
*/
/*取消http://www.12301.cc/meituan/a.php?orderid=1421554202&method=cancel&verifycode=793449&timestamp=1401088833&app_key=ea59253a4292cbff10e3bb1ba1db01a490aabcf0&sign=90ab3f8d04f4914e4d7ca5e4338ae0fe0bc961e3*/
/*查询http://www.12301.cc/meituan/MT_All_Order.php?orderid=1692446994&method=query&verifycodes=770456&timestamp=2014-05-31+00%3A00%3A00&app_key=ea59253a4292cbff10e3bb1ba1db01a490aabcf0&sign=2b4a885d3d98e117dd043a62aea4044e9986e6ad*/

$data=json_encode($_REQUEST,true);
$ip = GetIP();
// err("------------------------".date('Y-m-d H:i:s')."------------------------------");

// $sign_key = '4866a0b329f143279a9ec80a8247b014818db49f'; 
$ac='16ucom';
$pw='c33367701511b4f6020ec61ded352059';
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$s1=new SoapClient("http://open.12301.cc/openService/MXSE.wsdl",array('encoding' =>'UTF-8','cache_wsdl' => 0));

if (!class_exists("go_sql")){
include("/var/www/html/new/conf/le.je");
  $le=new go_sql();
  $le->connect();
}

$method = $_GET['method'];
$app_key = $_GET['app_key'];//唯一标识符

if($method=='send' && $app_key=='ea59253a4292cbff10e3bb1ba1db01a490aabcf0'){
	//这里是提交订单
	$phone =  $_GET['mobile']; //手机号码
	$orderid = $_GET['orderid']; //远端订单号
	$ids =  $_GET['outergoodsid'];//门票id
	$idss = explode('|',$ids);
	$landid = $idss[0]; //供应商id
	$lid = $idss[1]; //景区id
	$tid = $idss[2]; //门票id
	$num = $_GET['num'];//数量
	$playtime =strtotime($_GET['valid_start']); //预计游玩时间
	$playtime = date("Y-m-d",$playtime);
	$price = 100;
	
	$arr = array();
	for($i=1;$i<=$num;$i++)
	{
		$submit_array = array("ac"=>'200750',"pw"=>'6f3fbb18f77b34f5bc0a75e67b58af53',"in0"=>$lid,"in1"=>$tid,"in2"=>$orderid."_".$i,"in3"=>$price,"in4"=>1 ,"in5"=>$playtime,"in6"=>'美团',"in7"=>$phone,"in8"=>"","in9"=>1,"in10"=>2,"in11"=>0,"in12"=>"","in13"=>"","in14"=>0,"in15"=>0,"in16"=>$landid);
		$xml2=$s1->__soapCall("PFT_Order_Submit",$submit_array);
		$result=simplexml_load_string($xml2);
		if((string)$result->Rec->UUordernum){ 
			//下单成功赋值凭证码，或者出现1075远端订单号重复的情况
			$codesss=(string)$result->Rec->UUcode; 
			$arr[]=$codesss;
		} 
		else{	
			if((int)$result->Rec->UUerrorcode==122){
			//授信不足，提醒供应商给分销商授信
				$str="select mobile from pft_member where id='$landid' limit 1";			
				$GLOBALS['le']->query($str);
				$GLOBALS['le']->fetch_assoc();
				$aid_Tel=$GLOBALS['le']->f('mobile');
				if($aid_Tel){
					$s->Send_SMS_V($aid_Tel,"系统公告:您的分销商[【美团】北京三快在线科技有限公司]因授信不足下单失败，失败订单号:".$orderid.",请尽快补充授信后联系OTA商家补单，避免影响销量");
				}
			}
			err("--------".date('Y-m-d H:i:s')."------------------------");
			err($data."|".$ip);
			err($xml2);
			break;
		}
	}
	if($arr){
		$tmp['code']=200;
		$tmp['err_str']='success';
		$tmp['verifycodes']=$arr;
		err(json_encode($tmp)."|".$ip);
		echo json_encode($tmp);
	}else{ 
	    $str ='{"code":"300"}';
		echo $str;
	}
	
}elseif($method=='cancel' && $app_key=='ea59253a4292cbff10e3bb1ba1db01a490aabcf0'){
	//这里是取消订单
	$code = $_GET['verifycode']; //获取凭证码
	$code = str_replace("，",",",$code);
	$orderid = $_GET['orderid']."_1";
	$result = search_lid($orderid,$code,$s,$s1);
	foreach ($result->Rec as $k1=>$v1){
		$pt_ordernum = (int)$v1->UUordernum;
		$cancel = $s->Order_Change_Pro($pt_ordernum,0,-1,0,0,"","","");
		if($cancel == 100){
			//取消成功
			err($pt_ordernum."|200");
			$str ='{"code":"200"}';
			echo $str;		 
		}else{
			err("-----------".date('Y-m-d H:i:s')."------------------------------");
			err($data."|".$ip);
			err($cancel."|".$pt_ordernum);
			$str ='{"code":"300"}';
			echo $str;
		}
    }
}elseif($method=='query' && $app_key=='ea59253a4292cbff10e3bb1ba1db01a490aabcf0'){
	//这里是查询订单
	$code = $_GET['verifycodes'];
	$code = str_replace("，",",",$code);
    $orderid = $_GET['orderid']."_1";	
	// $result = search_lid($orderid,$code,$s,$s1);
	$result = Select_REMOorder($orderid,$s);
	$arr = array();
	foreach ($result->Rec as $k1=>$v1){
		$tmp = array();
		
		if(!empty($v1->UUcode))
		{	
			if((int)$v1->UUstatus == 0  || (int)$v1->UUstatus ==2){
				//未使用或者已过期
				$UUstatus = 0;
				$canceled_num = 0;
				$used_num = 0;				
			}elseif((int)$v1->UUstatus == 3){
				//取消
				$UUstatus = 2;
				$canceled_num = 1 ;
				$used_num = 0;
			}elseif((int)$v1->UUstatus == 7 || (int)$v1->UUstatus ==1 ){
				//已使用或者部分使用
				$UUstatus = 1;
				$used_num = 1;
				$canceled_num = 0;
			}
			$tmp['code']=(string)$v1->UUcode;
			$tmp['status']=$UUstatus;
			$tmp['used_num'] = $used_num;
			$tmp['canceled_num'] = $canceled_num;
		}
		$arr[] =$tmp;
	}
	if($arr){
		$str ='{"code":"200","err_str":"success","result":'.json_encode($arr).'}';
		echo $str;
	}
    else{
		$str ='{"code":"300","err_str":"fail"}';
		echo $str;
	}

}


function search_lid($orderid,$code,$s,$s1){
	$get_lid_date = $s1->__soapCall("Order_Globle_Search",array("ac"=>'200750',"pw"=>'6f3fbb18f77b34f5bc0a75e67b58af53',"in0"=>"","in1"=>"","in2"=>"","in3"=>"","in4"=>"","in5"=>"","in6"=>"","in7"=>"","in8"=>"","in9"=>"","in10"=>"","in11"=>"","in12"=>"","in13"=>$orderid,"in14"=>"","in15"=>"","in16"=>"","in17"=>"","in18"=>"","in19"=>"","in20"=>"","in21"=>1,"in23"=>"","in24"=>"","in25"=>"","in26"=>""));
	$get_lid_date_xml =simplexml_load_string($get_lid_date);
	if(empty($get_lid_date_xml)) exit;
	$lid= (int)($get_lid_date_xml->Rec->UUlid);
  	$get_code =$s->PFT_Order_Code("1853",$code,-1,$lid);
  	$result= simplexml_load_string($get_code);	
	return $result;
}
function Select_PFTorder($order){
		$pft_order = $this->inSide->Order_Globle_Search(
			0,0,0,0,"",
			"","","","","",
			"","",$order,"",0,
			"","","",1,0,
			0,1,"","","",
			"","","","","",
			"","",1,"",""
		);
		$pft_order_xml =simplexml_load_string($pft_order);
		return $pft_order_xml;
	}
function err($txt){
	$fp = fopen("debug.txt","a");
	flock($fp, LOCK_EX) ;
	fwrite($fp,$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}
function GetIP(){
	if(!empty($_SERVER["HTTP_CLIENT_IP"])){
		$cip = $_SERVER["HTTP_CLIENT_IP"];
	}elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
		$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	}elseif(!empty($_SERVER["REMOTE_ADDR"])){
		$cip = $_SERVER["REMOTE_ADDR"];
	}
	else{
	  $cip = "无法获取！";
	}
	return $cip;
}

?> 